package com.sproutcat.sample;

public class SynchronizedExample2 {

    private static Object obj = new Object();

    public static void main(String[] args) throws Exception {
        synchronized (obj) {
            newThread("t1");
            newThread("t2");
            newThread("t3");
            newThread("t4");
            newThread("t5");

            Thread.sleep(1000);
            System.out.println("main release");
        }

        System.out.println("main end");
        Thread.sleep(2000);
    }

    private static void newThread(String name) throws Exception {
        new Thread(() -> {
            synchronized (obj) {
                System.out.println(Thread.currentThread().getName() + " get");
            }
        }, name).start();
        Thread.sleep(100L);
    }

}
